using System;
using System.IO;
using CodePlex.TfsLibrary.ObjectModel;

namespace CodePlex.CodePlexClientLibrary
{
    [Command("resolve", "Resolves conflicted file(s)", MinArgs = 1, UsageSummary = "<path...> [options]",
        UsageDescription = @"This command will mark a conflicted file as being resolved so that it can
be committed. Note that it does not actually do any of the work of resolving
the conflict, which the user must do by hand (using the conflicted file
artifacts; i.e., the .mine and .r<#> files). It will also delete the those
artifacts from the working directory, if they still exist.

Users are encouraged to use the 'merge' command to help them resolve
conflicts in their source files.

If <path> is a directory, it will resolve all the conflicted files in that
directory (and all sub-directories, if /recursive is specified).")]
    public class ResolveCommand : NonAuthenticatedCommand
    {
        bool recursive;

        [Option("recursive", "act recursively on directories", ShortName = "e")]
        public bool Recursive
        {
            get { return recursive; }
            set { recursive = value; }
        }

        public override void Execute()
        {
            foreach (string path in Arguments)
                Engine.Resolve(CodePlexClientUtil.GetFullPath(path), recursive, OnSourceItem);
        }

        static void OnSourceItem(SourceItem item,
                                 SourceItemResult result)
        {
            if (result != SourceItemResult.S_Ok)
                ConsoleUtil.PrintCommandError(item.LocalName, result);
            else
                Console.WriteLine("Resolved '{0}'", CodePlexClientUtil.GetRelativePath(Directory.GetCurrentDirectory(), item.LocalName));
        }
    }
}